/**
 * Created by waterworld on 16-9-18.
 */
var P = require("bluebird");
var wilddogAsync = module.exports;

wilddogAsync.setInstance=function(wilddog)
{
    this.wilddog=wilddog;
};

wilddogAsync.getData= P.coroutine(function*(ref)
{
    var getDataAsync=new Promise(function(resolveFuncRet, rejectErr) {ref.once("value", resolveFuncRet);});
    var data=yield getDataAsync.then(function(data){ return data;});
    return data.val();
});

wilddogAsync.getDataAsync= P.coroutine(function*(path)
{
    if(!this.wilddog)
    { return null;
    }
    var ref=this.wilddog.sync().ref(path);
    var getDataAsync=new Promise(function(resolveFuncRet, rejectErr) {ref.once("value", resolveFuncRet);});
    var data=yield getDataAsync.then(function(data){ return data;});
    return data.val();
});



wilddogAsync.updateData= P.coroutine(function*(wilddog,path,value){
    var ref = wilddog.sync().ref(path);//获取节点
    var updateAsync=new Promise(function(resolveFuncRet, rejectErr) {ref.update(value, resolveFuncRet);});
    try
    {   var ret= yield updateAsync.then(function(err){ if(err==null) return true;else return false;});
        return ret;
    }
    catch(e)
    { return false;
    }
});


wilddogAsync.update= P.coroutine(function*(path,value){
    return;
    var ref = this.wilddog.sync().ref(path);//获取节点
    var updateAsync=new Promise(function(resolveFuncRet, rejectErr) {ref.update(value, resolveFuncRet);});
    try
    {   var ret= yield updateAsync.then(function(err){ if(err==null) return true;else return false;});
        return ret;
    }
    catch(e)
    { return false;
    }
});


wilddogAsync.removeAsync= P.coroutine(function*(path)
{
    var ref=this.wilddog.sync().ref(path);
    ref.remove();
});
